home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 31 / Amiga Format CD31 (1998-09-02)(Future Publishing)(GB)(Track 1 of 2)[!][issue 1998-10].iso / -seriously_amiga- / hardware / transadf / source / mem_chunks.c < prev    next >
C/C++ Source or Header  |  1998-07-20  |  2KB  |  101 lines

  1. /* mem_chunks.c - Simple routines that keep track of memory allocation.
  2. ** Copyright (C) 1998 Karl J. Ots
  3. ** 
  4. ** This program is free software; you can redistribute it and/or modify
  5. ** it under the terms of the GNU General Public License as published by
  6. ** the Free Software Foundation; either version 2 of the License, or
  7. ** (at your option) any later version.
  8. ** 
  9. ** This program is distributed in the hope that it will be useful,
  10. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12. ** GNU General Public License for more details.
  13. ** 
  14. ** You should have received a copy of the GNU General Public License
  15. ** along with this program; if not, write to the Free Software
  16. ** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  17. */
  18.  
  19. #include <exec/types.h>
  20. #include <exec/nodes.h>
  21. #include <exec/lists.h>
  22. #include <exec/memory.h>
  23. #include <clib/alib_protos.h>
  24. #include <clib/exec_protos.h>
  25.  
  26. struct List MemChunkList;
  27.  
  28. struct MemChunk {
  29.   struct Node mc_Node;
  30.   
  31.   ULONG  mc_ByteSize;
  32.   void  *mc_Chunk;
  33. };
  34.  
  35.  
  36. /*
  37. ** Initalise the Memory Chunk List ready for use
  38. */
  39. void
  40. initMemChunkList (void)
  41. {
  42.   NewList (&MemChunkList);
  43. }  
  44.  
  45. /*
  46. ** Clear the Memory Chunk List of all entries
  47. */
  48. void
  49. deleteMemChunkList (void)
  50. {
  51.   struct MemChunk *chunk;
  52.   
  53.   while (( chunk = (struct MemChunk *) RemTail (&MemChunkList) ))
  54.     FreeMem ((void *)chunk, (chunk->mc_ByteSize + sizeof (struct MemChunk)));
  55. }
  56.  
  57.  
  58. /*
  59. ** Allocate a block of memory and add it to the Memory Chunk List
  60. */
  61. void *
  62. myAllocMem (ULONG byteSize, ULONG attributes)
  63. {
  64.   struct MemChunk *chunk;
  65.   UBYTE *mem;
  66.   
  67.   mem = (UBYTE *)AllocMem ((byteSize + sizeof (struct MemChunk)), attributes);
  68.   if (mem)
  69.   {
  70.     chunk = (struct MemChunk *)mem;
  71.     mem += sizeof (struct MemChunk);
  72.     
  73.     chunk->mc_ByteSize = byteSize;
  74.     chunk->mc_Chunk = (void *)mem;
  75.     
  76.     AddTail (&MemChunkList, (struct Node *)chunk);
  77.   }
  78.   
  79.   return (void *)mem;
  80. }
  81.  
  82.  
  83. /*
  84. ** Remove a block from the Memory Chunk List and free it
  85. */
  86. void
  87. myFreeMem (void *memoryBlock)
  88. {
  89.   struct MemChunk *chunk;
  90.   
  91.   chunk = (struct MemChunk *)
  92.           (((UBYTE *)memoryBlock) - sizeof (struct MemChunk));
  93.   
  94.   /* Make sure this memory pointer is part of the chunk list */
  95.   if (memoryBlock == chunk->mc_Chunk)
  96.   {
  97.     Remove ((struct Node *)chunk);
  98.     FreeMem ((void *)chunk, (chunk->mc_ByteSize + sizeof (struct MemChunk)));
  99.   }
  100. }
  101.